package frames;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/** Implements the popup table menu
 * 
 * @author Konstantin
 * @version 1.0
 */
public class TableMenu extends JPopupMenu {
	
	private DatabaseTree dbTree;
	private Connection dbConnection;
	private TreePath pathToNode;
	
	/** Class constructor
	 * 
	 * @param tree represents the database systems tree
	 * @param path represents the path to the table node in the tree
	 * @param connection represents the established connection
	 */
	TableMenu(DatabaseTree tree, TreePath path, Connection connection) {
		dbTree = tree;
		pathToNode = path;
		dbConnection = connection;
		
		JMenuItem dropTableItem = new JMenuItem("Drop table");
		dropTableItem.addActionListener(new TableMenuHandler("Drop table"));
		this.add(dropTableItem);
		
		JMenuItem selectAllItem = new JMenuItem("View data");
		selectAllItem.addActionListener(new TableMenuHandler("View data"));
		this.add(selectAllItem);
		
		JMenuItem insert = new JMenuItem("Insert values");
		insert.addActionListener(new TableMenuHandler("Insert"));
		this.add(insert);
	}
	
	/** Implements a listener for the table menu options
	 * 
	 * @author Konstantin Dobrev
	 * @version 1.0
	 */
	private class TableMenuHandler implements ActionListener {

		private String command;
		
		/** Class constructor
		 * 
		 * @param commandName represents the value which will determine
		 * the invoked action
		 */
		TableMenuHandler(String commandName) {
			command = commandName;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			switch(command) {
			case "Drop table" : dropTable(); break;
			case "View data" : selectAll(); break;
			case "Insert" : insertValues(); break;
			}
		}
		 /**
		  * Drops the selected table from it's database and removes it
		  * form the database tree
		  * @exception SQLException
		  */
		private void dropTable() {
			int dropChoice = JOptionPane.showConfirmDialog(null, "Are you sure you want to drop the table '" + pathToNode.getLastPathComponent() + "'?");
			if(dropChoice != 0) return;
			try {
				Statement stm = dbConnection.createStatement();
				stm.execute("DROP TABLE " + pathToNode.getLastPathComponent());
				DefaultTreeModel model = (DefaultTreeModel) dbTree.getModel();
				DefaultMutableTreeNode tableNode = (DefaultMutableTreeNode) pathToNode.getLastPathComponent();
				model.removeNodeFromParent(tableNode);
			} catch (SQLException ex) {
				JOptionPane.showMessageDialog(null, ex.getMessage());
			}
		}
		
		/**
		 * Opens a frame with all the entered table data
		 */
		private void selectAll() {
			String dbName = pathToNode.getPathComponent(1).toString();
			String tableName = pathToNode.getLastPathComponent().toString();
			try {
				TableRecordsFrame tableFr = new TableRecordsFrame(dbConnection, dbName, tableName);
				tableFr.showTableRecords();
			} catch (SQLException ex) {
				JOptionPane.showMessageDialog(null, ex.getMessage());
			}
		}
		
		/**
		 * Opens a frame for insertion of data to the specific table
		 */
		private void insertValues() {
			String dbName = pathToNode.getPathComponent(1).toString();
			String tableName = pathToNode.getLastPathComponent().toString();
			InsertValuesFrame insertFr = new InsertValuesFrame(dbConnection, dbName, tableName);
		}
		
	}
	
	
	
	
	
	
	
	
	
	
}
